﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    /// <summary>
    /// 联系人
    /// </summary>
    public class BLL_contact
    {
        /// <summary>
        /// 增
        /// </summary>
        /// <param name="ct"></param>
        /// <returns></returns>
        public string insert(contact ct)
        {
            if (ct.contact_refrences_user_email != null)
            {
                GodHelperDBDataContext da = new GodHelperDBDataContext();
                da.contact.InsertOnSubmit(ct);
                da.SubmitChanges();
                string contactid = "" + ct.contact_id ;
                return contactid;
            }
            return null;
        }

        /// <summary>
        /// 删
        /// </summary>
        /// <param name="ct"></param>
        /// <returns></returns>
        public bool delete(contact ct)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            var p = da.contact.Where(item => item.contact_id == ct.contact_id);
            if (p.Any())
            {
                da.contact.DeleteOnSubmit(p.ToList()[0]);
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="ct"></param>
        /// <returns></returns>
        public bool update(contact ct)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            var p = da.contact.Where(item => item.contact_id == ct.contact_id);
            if (p.Any())
            {
                var var_contact = p.ToList()[0];
                var_contact.contact_name = ct.contact_name;
                var_contact.contact_phone = ct.contact_phone;
                var_contact.contact_image = ct.contact_image;
                var_contact.contact_time = ct.contact_time;
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="ct"></param>
        /// <param name="refrenceskey">根据那个键值查询</param>
        /// <returns></returns>
        public List<contact> search(contact ct, string refrenceskey)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            switch (refrenceskey)
            {
                case "contact_id":
                    if (ct.contact_id != null)
                    {
                        var p = da.contact.Where(item => item.contact_id == ct.contact_id);
                        if (p.Any())
                        {
                            return p.ToList();
                        }
                    }
                    break;
                case "contact_name":
                    if (ct.contact_name != null)
                    {
                        var p = da.contact.Where(item => item.contact_name == ct.contact_name && item.contact_refrences_user_email == ct.contact_refrences_user_email);
                        if (p.Any())
                        {
                            return p.ToList();
                        }
                    }
                    break;

                case "contact_refrences_user_email":
                    if (ct.contact_refrences_user_email != null)
                    {
                        var p = da.contact.Where(item => item.contact_refrences_user_email == ct.contact_refrences_user_email);
                        if (p.Any())
                        {
                            return p.ToList();
                        }
                    }
                    break;
            }
            return null;
        }
    }
}
